﻿//
// GNS_TickLayer.cpp
// GameHall
//  兑奖
//
// Created by mr.l on 16/2/3.
//
//

#include "GnsClientFrame.h"
#include "GNS_TickLayer.h"
#include "cocostudio/CocoStudio.h"
#include "GNS_PlayerMgr.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

enum 
{
	BTN_BACK,
	BTN_SEND,    //发送
};

GNS_TickLayer::GNS_TickLayer()
{
    m_RootNode=0;
}

GNS_TickLayer::~GNS_TickLayer()
{
    
}

GNS_TickLayer* GNS_TickLayer::createLayer()
{
    auto pGNS_TickLayer = GNS_TickLayer::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_TickLayer);
    return pGNS_TickLayer;
}

bool GNS_TickLayer::init()
{
    if ( !GNS_YJFLayer::init() )
    {
        return false;
    }
    return true;
}
void GNS_TickLayer::SetNodeName(std::string CsbName)
{
    m_RootNode = CSLoader::createNode(CsbName);
    m_RootNode->setContentSize(winsize);
    m_RootNode->setAnchorPoint(Vec2(0.5f, 0.5f));
    ui::Helper::doLayout(m_RootNode);
    addChild(m_RootNode,11);

    auto pBackBtn = static_cast<Button*>(m_RootNode->getChildByName("Button_4"));
	pBackBtn->setTag(BTN_BACK);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_TickLayer::BtnCallBack, this));

	auto btnSend = static_cast<Button*>(m_RootNode->getChildByName("Button_1"));
	btnSend->setTag(BTN_SEND);
	btnSend->addTouchEventListener(CC_CALLBACK_2(GNS_TickLayer::BtnCallBack, this));

	TextField *textGold = static_cast<TextField*>(m_RootNode->getChildByName("TextField_1"));
//	textGold->addEventListener(CC_CALLBACK_2(GNS_YJFLayer::TextFileCallBack, this)); 
}
void GNS_TickLayer::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
	if (type == ui::Widget::TouchEventType::ENDED) {
		GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
		assert(pGnsClientFrame);
		auto pButton = (Button*)pSender;
		int BtnTag = pButton->getTag();
		switch (BtnTag) {
		case BTN_BACK:
		{
			Show(false);
			break;
		}
		case BTN_SEND:
		{
			TextField *textGold = static_cast<TextField*>(m_RootNode->getChildByName("TextField_1"));
			//textGold->addEventListener(CC_CALLBACK_2(GNS_YJFLayer::TextFileCallBack, this));
			auto goldStr = textGold->getString();
			if (!CCGnsOften::isNumber(goldStr))
			{
				ShowCommonTips("只能输入数字！");
				return;
			}
			auto gold = atoi(textGold->getString().c_str());
			TextField *szText = static_cast<TextField*>(m_RootNode->getChildByName("TextField_2"));
			CMD_CSG_Exchange data;
			data._operType = 2;
			data._rechangeGold = gold;
			string mark = szText->getString();
			memcpy(data._remark, UTOG(mark.c_str()).c_str(), UTOG(mark.c_str()).length() + 1);
			data._revAgentCode = GNS_PlayerMgr::GetInstance()->getAgentID();
			data._sendAgentCode = GNS_PlayerMgr::GetInstance()->getMyUserIdx();

			pGnsClientFrame->sendMsg(HALLSERVER, MAIN_MSG_SHOP, SUB_CSG_REQUEST_EXCHANGE_GOLD_TO_RMB, (char*)&data, sizeof(data));
			Show(false);
			break;
		}
		default:
			break;
		}
    }
}